<?php
namespace app\admin\controller;

use app\common\model\Fileware as FilewareModel;
use think\File;
use think\Loader;
use think\Request;
use think\Session;
use app\common\controller\Base;
use app\common\model\Download as DownloadModel;
use app\common\model\DownloadCate as DownloadCateModel;

class Download extends Base{

    public function index(){
        $downloads = DownloadModel::where([])->order('id desc')->paginate(10);

        $category = DownloadCateModel::where([])->column('name,id,cover','id');

        $data = ['downloads'=>$downloads,'category'=>$category];

        return view('',$data);
    }

    /**
     * 实际上 download表只需要记录 文件的id就行了， 笨蛋。
     * @return \think\response\View
     */
    public function add(){
        $category = DownloadCateModel::where([])->select();

        $data = [];
        $data['category'] = $category;

        return view('',$data);
    }


    public function edit(Request $request){
        $id = $request->param('id');
        $download = DownloadModel::where('id',$id)->find();

        $category = DownloadCateModel::where([])->select();
        $data = [];
        $data['category'] = $category;

        return view('',['model'=>$download,'category'=>$category]);
    }

    public function create(Request $request){
        if(!$request->isPost()){
            $this->error("非法途径访问");
        }

        $data = $request->only(['title','desc','file_id','cate']);

        $data['title'] = htmlspecialchars($data['title']);
        $data['desc'] = htmlspecialchars($data['desc']);

        $downloadModel = DownloadModel::create($data);
        if($downloadModel){
            $this->success("操作成功");
        }else{
            $this->error("操作失败");
        }

    }

    /**
     * 删除掉以前的文件 免得浪费空间
     * 这是因为目前还没有做文件的删除功能 所以只能这样做，如果做了可以复用文件，和文件统一管理，那么就不需要这么做了。
     */
    public function update(Request $request){
        $post = $request->post();

        $validate = Loader::validate("DownloadValidate");
        if(!$validate->check($post)){
            $this->error($validate->getError());
        }

        $model = DownloadModel::where(['id'=>$post['id']])->find();
        //如果现在的文件跟以前的文件不一样  先找到老的 删除
        //如果是一样的，那么就不需要删除了
        if($post['file_id'] != $model->file_id){
            $file = FilewareModel::where(['id'=>$model->file_id])->find();
            if($file){
                $dir = ROOT_PATH.'public'.DS;
                $file = $dir.$file['url'];
                $res = @unlink($file);
                if($res){
                    FilewareModel::where(['id'=>$model->file_id])->delete();
                }
            }
        }

        $model->save($post);

        $this->success("更新成功");
    }


    public function delete(Request $request){
        $id = $request->param('id');

        $model = DownloadModel::where(['id'=>$id])->find();
        $file = FilewareModel::where(['id'=>$model->file_id])->find();
        if($file){
            $dir = ROOT_PATH.'public'.DS;
            $file = $dir.$file['url'];
            $res = @unlink($file);
            if($res){
                FilewareModel::where(['id'=>$model->file_id])->delete();
            }
        }

        $model->delete();
        $this->success("删除成功");
    }


    /**
     * @param Request $request
     * @return \think\response\View
     */
    public function category( Request $request ){
        $downloadCate = DownloadCateModel::where([])->select();

        return view( "",['downloadCate'=>$downloadCate] );
    }


    /**
     * @param Request $request
     * @return \think\response\View
     */
    public function categoryAdd( Request $request ){

        return view('',[]);
    }

    /**
     * @param Request $request
     * @return  \think\response\View
     */
    public function categoryEdit( Request $request ){
        $id = $request->param('id');

        $model = DownloadCateModel::where(['id'=>$id])->find();

        return view('',['model'=>$model]);
    }


    /**
     *
     * @param Request $request
     */
    public function createCategory(Request $request){

        $data = $request->only("name,cover","post");

        $validate = Loader::validate("DownloadCateValidate");

        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        $res = DownloadCateModel::create($data);
        if($res){
            $this->success("新增成功");
        }

        $this->error("操作失败");
    }

    /**
     * @param Request $request
     */
    public function updateCategory(Request $request){
        $data = $request->post();
        $validate = Loader::validate("DownloadCateValidate");
        if(!$validate->check($data)){
            $this->error($validate->getError());
        }

        $res = DownloadCateModel::where(['id'=>$data['id']])->update($data);
        if($res){
            $this->success("更新成功");
        }

        $this->error("更新失败");

    }


    public function deleteCategory(Request $request){
        $id = $request -> param("id");

        $exists = DownloadModel::where(['cate'=>$id])->find();
        if($exists){
            $this->error("该分类还有数据,请先删除数据");
        }

        $res = DownloadCateModel::where(['id'=>$id])->delete();
        if($res){
            $this->success("删除成功");
        }

        $this->error("删除失败");

    }


}